package cn.kgc.microservicecloudproviderdept.controller;

import cn.kgc.microservicecloudapi.pojo.Category;
import cn.kgc.microservicecloudapi.pojo.ResultEntity;
import cn.kgc.microservicecloudproviderdept.service.CategoryService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RequestMapping("/provider/category")
@RestController
public class CategoryController {

    @Resource
    CategoryService categoryService;

    @RequestMapping("/list")
    public ResultEntity<List<Category>> list(){
        List<Category> all = categoryService.getAll();
        return ResultEntity.successWithoutData(all);
    }

    @RequestMapping("/get/{id}")
    public ResultEntity<Category> get(@PathVariable Integer id){
        Category category = categoryService.getById(id);
        return ResultEntity.successWithoutData(category);
    }

    @RequestMapping("/update")
    public ResultEntity<String> update(@RequestBody Category category){
        int i = categoryService.updateByPrimaryKey(category);
        if (i>0){
            return ResultEntity.successWithoutData();
        }else{
            return ResultEntity.failed("修改失败");
        }
    }

    @RequestMapping("/insert")
    public ResultEntity<String> insert(@RequestBody Category category){
        int i = categoryService.insert(category);
        if (i>0){
            return ResultEntity.successWithoutData();
        }else{
            return ResultEntity.failed("添加失败");
        }
    }

    @RequestMapping("/delete/{id}")
    public ResultEntity<String> delete(@PathVariable Integer id){
        int i=categoryService.deleteByPrimaryKey(id);
        if (i>0){
            return ResultEntity.successWithoutData();
        }else{
            return ResultEntity.failed("删除失败");
        }
    }

}
